//
//  EFengShareNewCommentController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/3/15.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengShareNewCommentController.h"
#import "EFengShareNewCommentCell.h"
#import "CShareCommentMgr.h"
#import "CShareMgr.h"
#import "EFengShare.h"
#import "EFengContent.h"
#import "EFengShareNewCommentFrame.h"
#import "CXmppMgr.h"
#import "EFengShareDetailController.h"

@interface EFengShareNewCommentController ()<UITableViewDataSource, UITableViewDelegate>

StrongProperty CShareMgr *shareMgr;
StrongProperty CShareCommentMgr *commentMgr;

StrongProperty UITableView *tableView;
StrongProperty NSArray *dataArray;

@end

@implementation EFengShareNewCommentController

#pragma mark 懒加载
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)dealloc
{
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = CommonColor;
    
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    self.navigationItem.title = @"回复";
    
    
    
    self.shareMgr = [[CShareMgr alloc] init];
    self.commentMgr = [[CShareCommentMgr alloc] init];
    
    self.tableView.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64.0f);
    [self.view addSubview:self.tableView];
    
    NSArray *array = [self.commentMgr getAllUnreadShareComment];
    [self.commentMgr updateAllUnreadCommentToReadComment];
    
    
    NSMutableArray *arrayM = [NSMutableArray array];
    [array enumerateObjectsUsingBlock:^(EFengContent *obj, NSUInteger idx, BOOL *stop) {
        obj.shareContent = [self.shareMgr getShareContentWithShareId:obj.shareId];
        obj.imageName = [self.shareMgr getShareImageNameWithShareId:obj.shareId];
        EFengShareNewCommentFrame *comframe = [[EFengShareNewCommentFrame alloc] init];
        comframe.unReadComment = obj;
        [arrayM addObject:comframe];
    }];
    self.dataArray = arrayM;
    [self.tableView reloadData];
    [[NSNotificationCenter defaultCenter] postNotificationName:K_XMPPDelComment object:nil];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   EFengShareNewCommentFrame *comFrame = self.dataArray[indexPath.row];
    return comFrame.cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EFengShareNewCommentFrame *comFrame = self.dataArray[indexPath.row];
    
    EFengShareNewCommentCell *cell = [EFengShareNewCommentCell eFengShareNewCommentCellWithTableView:tableView];
    
    cell.unReadCommentFrame = comFrame;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    EFengShareNewCommentFrame *comFrame = self.dataArray[indexPath.row];
    
    EFengShareDetailController *detail = [[EFengShareDetailController alloc] init];
    detail.shareId = comFrame.unReadComment.shareId;
    [self.navigationController pushViewController:detail animated:YES];
}

@end
